import Server from '@/config/server';
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode: false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  columnData: [],
};
const actions = {
  // 导出简报列表
  equipmentExportData({ commit }, data) {
    const params = JSON.parse(JSON.stringify(data));
    delete params.page;
    return request.get(Server.action.bulletinRecordExport, { params, responseType: 'blob' });
  },
  // 列表
  getLists({ commit }, page) {
    request.get(Server.action.bulletinRecord_list, { params: page })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setLists', response.data.result.list);
          const {fields} = response.data.result;
          // fields.push({
          //     title: '操作',
          //     align: 'center',
          //     width: 60,
          //     scopedSlots: { customRender: 'action' },
          //     fixed: 'right',
          // })
          fields[0].width = 60;
          fields.forEach((item, i) => {
              fields[i].dataIndex = fields[i].key
              fields[i].align = 'center';
              if(i < fields.length-1) {
                fields[i].width = item.width || 120
              }
              if (item.key == "content") {
                  item.width = 480
              }
            });

          commit('setFields', fields);
        } else {

        }
      });
  },
  // 编辑
  dataEdit({ commit }, data) {
    commit('setErrorCode', false);
    request.get(Server.action.alarmRecord_handleStatus, { params: data })
      .then((response) => {
        console.log(response);
        commit('setErrorCode', response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result);
        } else {
          showError(response.data.message);
        }
      });
  },
  // 详情
  dataDetail({ commit }, id) {
    request.get(Server.action.alarmRecord_detail + id)
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit('setDetail', response.data.result);
        // showSuccess(response.data.result)
        } else {
          showError(response.data.message);
        }
      });
  },
  // ID删除 > 参数为：[1,2,3,4,5,6]
  dataDel({ commit }, ids) {
    console.log(ids);
    request.get(Server.action.bulletinRecord_delete, { params: { id: ids } })
      .then((response) => {
        console.log(response);
        if (response.data.errorCode === 0) {
          showSuccess('删除成功');
        } else {
          showError(response.data.message);
        }
      });
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
  },
  setLists(state, lists) {
	    state.dataLists = lists;
  },
  setFields(state, lists) {
	    state.Fields = lists;
  },
  setDetail(state, data) {
    state.itemDetail = data;
  },
  setColumnAll(state, data) {
    state.columnData = data;
  },

};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
};
